//
//  EpImageProcessor.m
//  BowmanMap2
//
//  Created by vtloc on 26/2/09.
//  Copyright 2009 bk. All rights reserved.
//

#import "EpImageProcessor.h"


@implementation EpImageProcessor

+(UIImage*) createImage:(UIImage*)image withMask:(UIImage*)maskImage {
	CGImageRef maskRef = maskImage.CGImage;
	
	CGImageRef mask = CGImageMaskCreate(
							CGImageGetWidth(maskRef), 
							CGImageGetHeight(maskRef), 
							CGImageGetBitsPerComponent(maskRef), 
							CGImageGetBitsPerPixel(maskRef),
							CGImageGetBytesPerRow(maskRef),
							CGImageGetDataProvider(maskRef),
							NULL, FALSE);
	CGImageRef maskedImage = CGImageCreateWithMask([image CGImage], mask);
	
	CGImageRelease(mask);
	CGImageRelease(maskRef);
	return [UIImage imageWithCGImage:maskedImage];
}
+(UIImage*)createSubImage:(UIImage*)source withRect:(CGRect)frame {
	CGImageRef subImage = CGImageCreateWithImageInRect(source.CGImage, frame);
	
	return [UIImage imageWithCGImage:subImage];
}
+(UIImage*)createCopy:(UIImage*)source {
	CGImageRef copy = CGImageCreateCopy(source.CGImage);
	
	return [UIImage imageWithCGImage:copy];
}
+(CGContextRef)createBitmapContextWithWidth:(int)pixelsWidth withHeight:(int)pixelsHeight {
	CGContextRef context = NULL;
	CGColorSpaceRef colorSpace = NULL;
	void* bitmapData;
	int bitmapBytesCount;
	int bitmapBytesPerRow;
	
	bitmapBytesPerRow = pixelsWidth * 4;
	bitmapBytesCount = pixelsHeight * bitmapBytesPerRow;

	colorSpace = CGColorSpaceCreateDeviceRGB(); 
    bitmapData = malloc( bitmapBytesCount ); 
    if (bitmapData == NULL) 
    { 
        return NULL; 
    } 
	
	context = CGBitmapContextCreate(bitmapData, 
									pixelsWidth, 
									pixelsHeight,
									8, 
									bitmapBytesPerRow, 
									colorSpace, 
									kCGImageAlphaPremultipliedLast);
	if(context==NULL){
		return NULL;
	}
	
	CGColorSpaceRelease(colorSpace);
	
	return context;
}
+(CGContextRef)createBitmapContextFromExistingImage:(UIImage*)source {
	CGImageRef imageRef = source.CGImage;
	int width = CGImageGetWidth(imageRef);
	int height = CGImageGetHeight(imageRef);

	CGContextRef context = [EpImageProcessor createBitmapContextWithWidth:width withHeight:height];
	CGRect imageFrame = CGRectMake(0,0,width,height);
	CGContextDrawImage(context,imageFrame, imageRef);
	
	return context;
}
+(UIImage*)createImageFromContext:(CGContextRef)context {
	CGImageRef image = CGBitmapContextCreateImage(context);
	
	return [UIImage imageWithCGImage:image];
}
+(UIImage*)filterImage:(UIImage*)source withFilter:(ImageFilterType)type {
	CGImageRef imageRef = source.CGImage;
	int width = CGImageGetWidth(imageRef);
	int height = CGImageGetHeight(imageRef);
	int totalPixels = width*height;
	
	CGContextRef context = [EpImageProcessor createBitmapContextWithWidth:width withHeight:height];
	CGRect imageFrame = CGRectMake(0,0,width,height);
	CGContextDrawImage(context,imageFrame, imageRef);

	CGImageAlphaInfo info = CGImageGetAlphaInfo(imageRef);
	BOOL hasAlpha = (	(info == kCGImageAlphaPremultipliedLast) 
					 || (info == kCGImageAlphaPremultipliedFirst) 
					 || (info == kCGImageAlphaLast) 
					 || (info == kCGImageAlphaFirst) 
					 ? YES 
					 : NO);
	
	if(hasAlpha) {
		if(info == kCGImageAlphaPremultipliedLast) {
			NSLog(@"kCGImageAlphaPremultipliedLast");
		} else if(info == kCGImageAlphaPremultipliedFirst) {
			NSLog(@"kCGImageAlphaPremultipliedFirst");
		} else if(info == kCGImageAlphaLast) {
			NSLog(@"kCGImageAlphaLast");
		} else if(info == kCGImageAlphaFirst) {
			NSLog(@"kCGImageAlphaFirst");
		}
	}
	
	size_t bpp = CGImageGetBitsPerComponent(imageRef);
	NSLog(@"bpp = %d",bpp);
	
	unsigned char* data = CGBitmapContextGetData(context);
	if(data!=NULL) {
		for(int offset=0;offset<totalPixels*4;offset+=4) {
			if(offset < totalPixels) {
				// for test purpose
				data[offset] = 255;			// red
				data[offset+1] = 255;		// green
				data[offset+2] = 255;		// blue
				data[offset+3] = 255;
			} else if(offset >totalPixels*3) {
				data[offset] = 0;
				data[offset+1] = 0;
				data[offset+2] = 0;
				data[offset+3] = 100;
			} else {
				data[offset] = 255;
				data[offset+1] = 255;
				data[offset+2] = 0;
				data[offset+3] = 0;
			}
		}
	}
	
	CGImageRelease(imageRef);
	imageRef = CGBitmapContextCreateImage(context);
	CGContextRelease(context);
	
	return [UIImage imageWithCGImage:imageRef];
}
@end
